<?php
namespace models\repositories;
use Doctrine\ORM\EntityRepository;
use entities;
class PerpustakaanRepository extends EntityRepository {

	/**
     * @inheritdoc
     */
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $orderBy = $orderBy === null ? array('id' => 'desc') : $orderBy;
        return parent::findBy($criteria, $orderBy, $limit, $offset);
    }
	
	public function findAll($orderBy=null){		
		return $this->findBy(array(), $orderBy);
	}
	
	public function findCBOByPerpustakaan($perpusId){
		$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
		$rsm->addScalarResult("mail", "mail");
		$rsm->addScalarResult("msisdn", "msisdn");
		$rsm->addScalarResult("user_id", "user_id");
		
		$query = $this->_em->createNativeQuery("
											select user.user_id, user.mail, user.msisdn from
											  perpustakaan 
											  inner join desa ON perpustakaan.desa_id = desa.id 
											  inner join distrik on desa.distrik_id = distrik.id
											  inner join usercbo_map_distrik umd on umd.distrik_id = distrik.id
											  inner join user on user.user_id = umd.user_id 
											where perpustakaan.id = '".$perpusId."';", $rsm);
		$str = $query->getResult();
		//echo $query->getSQL();
		//echo "<pre>";print_r($str);die();		
		return $str;											 
	}
}

